//
//  YBGoodWorking.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/7/18.
//

#import "YBGoodWorking.h"
#import "AFNetworking.h"


@implementation YBGoodWorking
+ (instancetype)share{
    static YBGoodWorking *netWorking;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        netWorking = [[self alloc] init];
        netWorking.goodmanager = [AFHTTPSessionManager manager];
        
        netWorking.goodmanager.responseSerializer = [AFHTTPResponseSerializer serializer];
        netWorking.goodmanager.requestSerializer.timeoutInterval = 15;
        // 响应类型
        netWorking.goodmanager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain",@"image/png", @"multipart/form-data",@"image/jpeg",@"application/octet-stream",@"application/xml",@"text/event-stream",nil];
    });
    return netWorking;
}

+(void)postWithUrl:(NSString *)url Dic:(NSMutableDictionary *)dic Suc:(PullGoodSuccessBlock)sucBack{
    NSData *data= [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];


    NSMutableDictionary * info = [NSMutableDictionary new];
    info[@"app_id"] = @"5574056";
    int a =[Tools getCurrentTimeStamp];
    info[@"timestamp"] = @(a);
    info[@"version"] = @"1";
    
    info[@"req_id"] = @"214713";
    info[@"data"] = jsonStr;
    NSString * md5 = [NSString stringWithFormat:@"app_id=%@&data=%@&req_id=%@&timestamp=%@qbD10VpiRE9PKVbu27JKKSmeLBffSlBtAGvb5pf-_IM=",info[@"app_id"],info[@"data"],info[@"req_id"],info[@"timestamp"]];
//    NSString * md51 = [NSString stringWithFormat:@"app_id=5159653&data=a02417d2-9604-11ec-b909-0242ac120002&timestamp=16468094803876803d823b468bc59dfa7ccc1b6ead"];
//    NSLog(@"%@",[Tools MD5WithString:md51]);
    
//    NSData *data1= [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:nil];
//    NSString *jsonStr1=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//
    info[@"sign"] = [Tools MD5WithString:md5];//@"69bae52eb39810b0248438964983824f"; 
    info[@"sign_type"] = @"MD5";
    NSString * str = [NSString stringWithFormat:@"https://ecom.pangolin-sdk-toutiao.com/%@",url];
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:str parameters:nil error:nil];
    request.timeoutInterval = 10.f;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info options:0 error:&error];
    if (jsonData) {
        // jsonData 是转换后的NSData对象
    } else {
        // 处理错误
    }

    [request setHTTPBody:jsonData];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 15;
    // 响应类型
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json",nil];
    
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (!error) {
            NSString *jsonStr2=[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSDictionary *responseObjects = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
            // 请求成功数据处理、
            if([responseObjects[@"code"] intValue] == 200){
                sucBack(responseObjects[@"data"],[responseObjects[@"code"] intValue],responseObjects[@"msg"]);
            }else{
                sucBack(responseObjects[@"data"],[responseObjects[@"code"] intValue],responseObjects[@"msg"]);
            }
        } else {
            sucBack(nil,-1001,@"网络超时~");
        }
    }];
    [task resume];
}

@end
